package com.studyroom.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyroom.dto.Result;
import com.studyroom.dto.UserDTO;
import com.studyroom.entity.Note;
import com.studyroom.entity.User;
import com.studyroom.mapper.NoteMapper;
import com.studyroom.mapper.UserMapper;
import com.studyroom.service.NoteService;
import com.studyroom.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.studyroom.utils.RedisContants.CONTACTS_KEY;


@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements NoteService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserMapper userMapper;
    @Override
    public Result getByUserId(Long id) {
        //获取当前用户信息
        UserDTO me=UserHolder.getUser ();
        //校验查找人信息
        //是否是本人
        if(me.getId ()==id){
            //本人,返回所有笔记
            List<Note> noteList= query().eq ("user_id",id).list ();
            return Result.ok (noteList);
        }
        //不是本人,从数据库查询是否是目标好友
        User user=userMapper.getUserByUserRealtion (id,me.getId ());

        if(user!=null){
                //是目标用户的好友,返回仅好友可见的note 0 1
                List<Note> noteList=query ().eq ("user_id",id).le ("authority",1).list ();
                return Result.ok (noteList);
            }

        //否则,是陌生人,返回所有人可见的note 0
        Map<String,Object> map=new HashMap<> ();
        map.put ("user_id",id);
        map.put ("authority",0);
        List<Note> noteList=query ().eq ("user_id",id).eq ("authority",0).list ();
        return Result.ok (noteList);
    }




    /*添加或更新一个Note*/
    @Override
    public Result addOrUpdate(Note note) {
        UserDTO me=UserHolder.getUser ();
        //数据库是否存在
        if(note.getId ()==null||note.getId ()<1){
            //id为空,数据不存在,添加到数据库
            if(note.getTitle ().length ()<1||note.getTitle ().length ()>20){
                return Result.fail ("标题长度为 1 ~ 20 位");
            }
            note.setUserId (me.getId ());
            note.setCreated (LocalDateTime.now ());
            boolean b=save (note);
            if(!b){
                return Result.fail ("添加失败");
            }
            return Result.ok ();
        }
        //id存在,更新数据库
        //标题长度
        if(note.getTitle ().length ()<1||note.getTitle ().length ()>20){
            return Result.fail ("标题长度为 1 ~ 20 位");
        }
        //验证是否有权修改
        if(!note.getUserId ().equals (me.getId ())){
            return Result.fail ("你无权修改");
        }
        //有权,更新到数据库
        boolean b=updateById (note);
        if(!b){
            return Result.fail ("更新失败");
        }
        return Result.ok ();
    }

    @Override
    public Result delById(Long id) {
        boolean b=removeById (id);
        if(!b){
            return Result.fail ("删除失败");
        }
        return Result.ok ();
    }
}
